package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.ContactNote;
import com.kuiandjie.entity.ContactNoteMsg;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.ConcatService;
import com.kuiandjie.service.ContactNoteService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.util.DateUtil;

public class ConteactNoteServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Project curp = (Project) session.getAttribute("curp");
		String id = request.getParameter("id");
		
		if(curp != null && id != null && !("").equals(id)) {
			int contactId = Integer.parseInt(id);
			
			if("GET".equals(request.getMethod())) {
				ContactNoteMsg cnm = new ContactNoteMsg();
				cnm.setContact(new ConcatService().findContactById(contactId));
				cnm.setList(new ContactNoteService().findByContactId(contactId));
				
				request.setAttribute("cnm", cnm);
				request.getRequestDispatcher("WEB-INF/views/allcontactnote.jsp").forward(request, response);
			} else {
				int noteId = Integer.parseInt(request.getParameter("noteid"));
				String note = request.getParameter("note");
				String lastTime = request.getParameter("time");
				Employee e = (Employee) session.getAttribute("employee");
				String name = request.getParameter("name");
				ContactNote cn = new ContactNote();
				cn.setContactId(contactId);
				cn.setContent(note);
				cn.setCreateTime(DateUtil.getNow());
				cn.setEmployeeId(e.getId());
				ContactNoteService cns = new ContactNoteService();
				cns.insertNote(cn);
				
				Log log = new Log();
				log.setDesc("联系了<a href='contactnote.jspx?id=" + contactId + "'>" + name + "</a>");
				log.setEmployeeId(e.getId());
				log.setProjectId(curp.getId());
				log.setTime(DateUtil.getNow());
				log.setName(e.getName());
				log.setPic(e.getPic());
				
				LogService ls = new LogService();
				ls.inserLog(log);
				
				request.setAttribute("msg", new Gson().toJson(log));
				request.getRequestDispatcher("msgflow.jspx").include(request, response);
				
				List<ContactNote> list = cns.getNoteByIdAndTime(contactId, noteId, lastTime);
				
				response.setContentType("application/json;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				
				out.print(new Gson().toJson(list));
				out.flush();
				out.close();
			}
			
		} else {
			response.sendRedirect("project.jspx");
		}
	}
}
